package com.pdd.controller;

import com.pdd.beans.Category;
import com.pdd.dto.DTO;
import com.pdd.service.CategoryService;
import com.pdd.util.UploadUtil;
import com.pdd.vo.category.CategoryAddVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(description = "商品分类接口",tags = {"商品分类接口"})
@CrossOrigin(origins = { "*" })
@Controller
public class CategoryController {

    @Autowired
    @Setter
    private CategoryService categoryService;

    @ApiOperation(value = "添加商品分类")
    @RequestMapping(value = "/category", method = RequestMethod.POST)
    @ResponseBody
    public DTO add(MultipartFile logo, CategoryAddVO vo, HttpServletRequest request){
        // 1. 执行上传，获取上传后的文件名
        String targetName = UploadUtil.upload(logo, request);

        // 2. 封装参数
        Category category = new Category();
        category.setPid(vo.getPid());
        category.setName(vo.getName());
        category.setOrder(vo.getOrder());
        category.setLogo(targetName);

        // 2. 执行新增
        boolean result = categoryService.addCategory(category);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }
    @ApiOperation(value = "删除商品分类")
    @RequestMapping(value = "/category/{id}", method = RequestMethod.DELETE)
    public DTO del(@PathVariable(value = "id") Integer id){

        boolean result = categoryService.deleteCategory(id);
        if (result){
            return DTO.success();
        }else {
            return DTO.error();
        }
    }

    @ApiOperation(value = "更新商品分类")
    @RequestMapping(value = "/category", method = RequestMethod.PUT)
    @ResponseBody
    public DTO upl(MultipartFile logo, CategoryAddVO vo, HttpServletRequest request){
        // 1. 执行上传，获取上传后的文件名
        String targetName = UploadUtil.upload(logo, request);

        // 2. 封装参数
        Category category = new Category();
        category.setPid(vo.getPid());
        category.setName(vo.getName());
        category.setOrder(vo.getOrder());
        category.setLogo(targetName);

        // 2. 执行新增
        boolean result = categoryService.updateCategory(category);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }

    @ApiOperation(value = "获取所有一级分类")
    @RequestMapping(value = "/category", method = RequestMethod.GET)
    @ResponseBody
    public DTO get(){
        // 1. 执行查询操作
        List<Category> list = categoryService.getByPid(null);

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }

    @ApiOperation(value = "获取指定分类下的所有二级分类")
    @RequestMapping(value = "/category/{pid}", method = RequestMethod.GET)
    @ResponseBody
    public DTO getByPid(@ApiParam(value = "父级分类编号") @PathVariable(value = "pid") Integer pid){
        // 1. 查询
        List<Category> list = categoryService.getByPid(pid);

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }

}
